package com.softtown.modules.workflow.service.impl;

import com.softtown.common.enums.IsDeleteEnum;
import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.common.utils.ConvertUtils;
import com.softtown.enums.SwfLastAprvCondTypeEnum;
import com.softtown.enums.SwfLastAprvReqEnum;
import com.softtown.enums.SwfLastAprvReqTypeEnum;
import com.softtown.modules.dapai.entity.CompSaleOrderEntity;
import com.softtown.modules.workflow.dao.SwfLastAprvReqDao;
import com.softtown.modules.workflow.dto.*;
import com.softtown.modules.workflow.entity.SwfDocPhaseDefEntity;
import com.softtown.modules.workflow.entity.SwfLastAprvReqEntity;
import com.softtown.modules.security.user.SecurityUser;
import com.softtown.modules.workflow.service.SwfDocPhaseDefService;
import com.softtown.modules.workflow.service.SwfLastAprvReqService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: Dingyayong
 * @Date: 2019/9/23 15:03
 * @Description:
 */
@Service
public class SwfLastAprvReqServiceImpl extends BaseServiceImpl<SwfLastAprvReqDao, SwfLastAprvReqEntity> implements SwfLastAprvReqService {

    @Resource
    private SwfDocPhaseDefService swfDocPhaseDefService;

    /**
     * 根据公司和审批段id查找出差审批的部门要求
     *
     * @param compId  公司id
     * @param phaseId 审批段id
     * @return com.softtown.modules.entity.SwfLastAprvReqEntity
     * @Author Dingyayong
     * @Date 2020/4/13 17:43
     */
    @Override
    public SwfLastAprvReqEntity findLastAprvReqByDeptAndTravel(Long compId, Long phaseId) {
        return baseDao.findLastAprvReqByDeptAndTravel(compId, phaseId);
    }

    /**
     * 根据公司和审批段id查找部门要求
     *
     * @param compId  公司id
     * @param phaseId 审批段id
     * @param docType 类型
     * @return com.softtown.modules.entity.SwfLastAprvReqEntity
     * @Author Dingyayong
     * @Date 2020/4/13 17:44
     */
    @Override
    public SwfLastAprvReqEntity findLastAprvReqByPhaseAndDocType(Long compId, Long phaseId, String docType) {
        return baseDao.findLastAprvReqByPhaseAndDocType(compId, phaseId, docType);
    }

    /**
     * 根据公司和审批段id查找报销审批的部门要求
     *
     * @param compId  公司id
     * @param phaseId 审批段id
     * @return com.softtown.modules.entity.SwfLastAprvReqEntity
     * @Author Dingyayong
     * @Date 2020/4/13 17:45
     */
    @Override
    public SwfLastAprvReqEntity findLastAprvReqByDeptAndExpense(Long compId, Long phaseId) {
        return baseDao.findLastAprvReqByDeptAndExpense(compId, phaseId);
    }

    /**
     * 根据公司和审批段id,条件为费目来查查找出差审批的岗位要求
     *
     * @param compId  公司id
     * @param phaseId 审批段id
     * @return java.util.List<com.softtown.modules.entity.SwfLastAprvReqEntity>
     * @Author Dingyayong
     * @Date 2020/4/13 17:46
     */
    @Override
    public List<SwfLastAprvReqEntity> findLastAprvReqByItemAndTravel(Long compId, Long phaseId) {
        return baseDao.findLastAprvReqByItemAndTravel(compId, phaseId);
    }

    /**
     * 根据公司和审批段id,条件为费目来查查找报销审批的岗位要求
     *
     * @param compId  公司id
     * @param phaseId 审批段id
     * @return java.util.List<com.softtown.modules.entity.SwfLastAprvReqEntity>
     * @Author Dingyayong
     * @Date 2020/4/13 17:47
     */
    @Override
    public List<SwfLastAprvReqEntity> findLastAprvReqByItemAndExpense(Long compId, Long phaseId) {
        return baseDao.findLastAprvReqByItemAndExpense(compId, phaseId);
    }

    /**
     * 根据公司和审批段要求
     *
     * @param compId       公司id
     * @param docTypeValue 单据类型
     * @param phaseId      审批段id
     * @param reqType      资格要求类型
     * @param condType     条件类型
     * @return java.util.List<com.softtown.modules.entity.SwfLastAprvReqEntity>
     * @Author Dingyayong
     * @Date 2020/4/13 17:47
     */
    @Override
    public List<SwfLastAprvReqEntity> findLastAprvReqByReqAndCondType(Long compId, String docTypeValue, Long phaseId, String reqType, String condType) {
        return baseDao.findLastAprvReqByReqAndCondType(compId, docTypeValue, phaseId, reqType, condType);
    }

    /**
     * 根据公司和审批段id,条件为金额来查查找出差审批的岗位要求
     *
     * @param compId  公司id
     * @param phaseId 审批段id
     * @return java.util.List<com.softtown.modules.entity.SwfLastAprvReqEntity>
     * @Author Dingyayong
     * @Date 2020/4/13 17:49
     */
    @Override
    public List<SwfLastAprvReqEntity> findLastAprvReqByMoneyAndTravel(Long compId, Long phaseId) {
        return baseDao.findLastAprvReqByMoneyAndTravel(compId, phaseId);
    }

    /**
     * 根据公司和审批段id,条件为金额来查查找报销审批的岗位要求
     *
     * @param compId  公司id
     * @param phaseId 审批段id
     * @return java.util.List<com.softtown.modules.entity.SwfLastAprvReqEntity>
     * @Author Dingyayong
     * @Date 2020/4/13 17:49
     */
    @Override
    public List<SwfLastAprvReqEntity> findLastAprvReqByMoneyAndExpense(Long compId, Long phaseId) {
        return baseDao.findLastAprvReqByMoneyAndExpense(compId, phaseId);
    }


    @Override
    public SwfLastAprvAddDaPaiDTO getDaPaiAprvReqInfo(Long compId, String docTypeValue) {
        //整个页面数据
        SwfLastAprvAddDaPaiDTO swfLastAprvAddDaPaiDTO = new SwfLastAprvAddDaPaiDTO();
        //整个tableData 数据
        List<SwfDocPhaseDefEntity> swfDocPhaseDefEntities = swfDocPhaseDefService.getDocPhaseDefListByDocType(compId, docTypeValue);
        List<SwfLastAprvDaPaiAddDataDTO> list =  ConvertUtils
                .sourceToTarget(swfDocPhaseDefEntities, SwfLastAprvDaPaiAddDataDTO.class);
        swfLastAprvAddDaPaiDTO.setDaPaiAddDataDTOList(list);
        return swfLastAprvAddDaPaiDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDaPaiAprvReq(SwfLastAprvAddDaPaiDTO swfLastAprvAddDaPaiDTO) {
        List<SwfLastAprvReqEntity> swfLastAprvReqEntities
                = baseDao.getSwfLastAprvReqInfo(swfLastAprvAddDaPaiDTO.getCompId(), swfLastAprvAddDaPaiDTO.getDocTypeValue());
        for (SwfLastAprvReqEntity swfLastAprvReqEntity : swfLastAprvReqEntities) {
            swfLastAprvReqEntity.setIsDeleted(IsDeleteEnum.DELETE.value());
            swfLastAprvReqEntity.setUpdater(SecurityUser.getUserId());
            swfLastAprvReqEntity.setUpdateDate(new Date());
            swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            baseDao.updateLastAprvReq(swfLastAprvReqEntity);
        }
        List<SwfDocPhaseDefEntity> swfDocPhaseDefEntities =
                swfDocPhaseDefService.getDocPhaseDefListByDocType(swfLastAprvAddDaPaiDTO.getCompId(), swfLastAprvAddDaPaiDTO.getDocTypeValue());
        for (SwfDocPhaseDefEntity swfDocPhaseDefEntity : swfDocPhaseDefEntities) {
            swfDocPhaseDefEntity.setIsDeleted(IsDeleteEnum.DELETE.value());
            swfDocPhaseDefEntity.setUpdater(SecurityUser.getUserId());
            swfDocPhaseDefEntity.setUpdateDate(new Date());
            swfDocPhaseDefEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            swfDocPhaseDefService.updateDocPhaseDef(swfDocPhaseDefEntity);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDaPaiAprvReq(SwfLastAprvAddDaPaiDTO swfLastAprvAddDaPaiDTO) {
        int i = 1;
        for (SwfLastAprvDaPaiAddDataDTO swfLastAprvDaPaiAddDataDTO : swfLastAprvAddDaPaiDTO.getDaPaiAddDataDTOList()) {
            //审批段定义
            SwfDocPhaseDefEntity swfDocPhaseDefEntity = new SwfDocPhaseDefEntity();
            swfDocPhaseDefEntity.setIsDeleted(0);
            swfDocPhaseDefEntity.setPhaseId(swfLastAprvDaPaiAddDataDTO.getApproval());
            swfDocPhaseDefEntity.setDocTypeValue(swfLastAprvAddDaPaiDTO.getDocTypeValue());
            swfDocPhaseDefEntity.setCompId(swfLastAprvAddDaPaiDTO.getCompId());
            swfDocPhaseDefEntity.setEmplId(swfLastAprvDaPaiAddDataDTO.getEmplId());
            swfDocPhaseDefEntity.setUserId(swfLastAprvDaPaiAddDataDTO.getUserId());
            swfDocPhaseDefEntity.setSortNum(i);
            swfDocPhaseDefEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            swfDocPhaseDefService.insert(swfDocPhaseDefEntity);
            i++;
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editDaPaiAprvReq(SwfLastAprvAddDaPaiDTO swfLastAprvAddDaPaiDTO) {
        List<SwfLastAprvReqEntity> swfLastAprvReqEntities
                = baseDao.getSwfLastAprvReqInfo(swfLastAprvAddDaPaiDTO.getCompId(), swfLastAprvAddDaPaiDTO.getDocTypeValue());
        for (SwfLastAprvReqEntity swfLastAprvReqEntity : swfLastAprvReqEntities) {
            swfLastAprvReqEntity.setIsDeleted(IsDeleteEnum.DELETE.value());
            swfLastAprvReqEntity.setUpdater(SecurityUser.getUserId());
            swfLastAprvReqEntity.setUpdateDate(new Date());
            swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            baseDao.updateLastAprvReq(swfLastAprvReqEntity);
        }
        List<SwfDocPhaseDefEntity> swfDocPhaseDefEntities =
                swfDocPhaseDefService.getDocPhaseDefListByDocType(swfLastAprvAddDaPaiDTO.getCompId(), swfLastAprvAddDaPaiDTO.getDocTypeValue());
        for (SwfDocPhaseDefEntity swfDocPhaseDefEntity : swfDocPhaseDefEntities) {
            swfDocPhaseDefEntity.setIsDeleted(IsDeleteEnum.DELETE.value());
            swfDocPhaseDefEntity.setUpdater(SecurityUser.getUserId());
            swfDocPhaseDefEntity.setUpdateDate(new Date());
            swfDocPhaseDefEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            swfDocPhaseDefService.updateDocPhaseDef(swfDocPhaseDefEntity);
        }
        int i = 1;
        for (SwfLastAprvDaPaiAddDataDTO swfLastAprvDaPaiAddDataDTO : swfLastAprvAddDaPaiDTO.getDaPaiAddDataDTOList()) {
            //审批段定义
            SwfDocPhaseDefEntity swfDocPhaseDefEntity = new SwfDocPhaseDefEntity();
            swfDocPhaseDefEntity.setIsDeleted(0);
            swfDocPhaseDefEntity.setPhaseId(swfLastAprvDaPaiAddDataDTO.getApproval());
            swfDocPhaseDefEntity.setDocTypeValue(swfLastAprvAddDaPaiDTO.getDocTypeValue());
            swfDocPhaseDefEntity.setCompId(swfLastAprvAddDaPaiDTO.getCompId());
            swfDocPhaseDefEntity.setEmplId(swfLastAprvDaPaiAddDataDTO.getEmplId());
            swfDocPhaseDefEntity.setUserId(swfLastAprvDaPaiAddDataDTO.getUserId());
            swfDocPhaseDefEntity.setSortNum(i);
            swfDocPhaseDefEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            swfDocPhaseDefService.insert(swfDocPhaseDefEntity);
            i++;
        }
    }

    /**
     * 添加最终审设置
     *
     * @param swfLastAprvReqAddDTO 添加终审者资格要求DTO
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 17:50
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addSwfLastAprvReq(SwfLastAprvReqAddDTO swfLastAprvReqAddDTO) {
        int i = 1;
        for (SwfLastAprvReqAddDataDTO swfLastAprvReqAddDataDTO : swfLastAprvReqAddDTO.getTableData()) {
            //部门要求
            if (swfLastAprvReqAddDataDTO.getData().getRadio().equals(SwfLastAprvReqEnum.DEPTREQNO.value())) {
                SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTO.getCompId());
                swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.DEPTREQ.value());
                swfLastAprvReqEntity.setRequirement1(SwfLastAprvReqEnum.DEPTREQNO.value());
                swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTO.getDocTypeValue());
                swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                baseDao.insert(swfLastAprvReqEntity);
            }
            if (swfLastAprvReqAddDataDTO.getData().getRadio().equals(SwfLastAprvReqEnum.DEPTPARENT.value())) {
                SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                swfLastAprvReqEntity.setRequirement1(SwfLastAprvReqEnum.DEPTPARENT.value());
                swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTO.getDocTypeValue());
                swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTO.getCompId());
                swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.DEPTREQ.value());
                swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                baseDao.insert(swfLastAprvReqEntity);
            }
            if (swfLastAprvReqAddDataDTO.getData().getRadio().equals(SwfLastAprvReqEnum.DEPTBYSELF.value())) {
                StringBuilder buf = new StringBuilder();
                SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTO.getDocTypeValue());
                swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.DEPTREQ.value());
                swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTO.getCompId());
                swfLastAprvReqEntity.setRequirement1(SwfLastAprvReqEnum.DEPTBYSELF.value());
                for (Long dept : swfLastAprvReqAddDataDTO.getData().getDeptIdList()) {
                    buf.append(dept.toString().concat(","));
                }
                swfLastAprvReqEntity.setRequirement2(buf.toString().substring(0, buf.toString().length() - 1));
                swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                baseDao.insert(swfLastAprvReqEntity);
            }
            //岗位要求
            //费目要求
            if (swfLastAprvReqAddDataDTO.getData().getCostData().size() > 0) {
                for (SwfLastAprvReqAddCostDTO swfLastAprvReqAddCostDTO : swfLastAprvReqAddDataDTO.getData().getCostData()) {
                    SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                    swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTO.getDocTypeValue());
                    swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTO.getCompId());
                    swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                    swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.POSNREQ.value());
                    swfLastAprvReqEntity.setCondType(SwfLastAprvCondTypeEnum.COSTREQ.value());
                    swfLastAprvReqEntity.setPrtyLevel(swfLastAprvReqAddDataDTO.getData().getCostNum());
                    if (swfLastAprvReqAddCostDTO.getCostItem().size() > 1) {
                        StringBuilder buf = new StringBuilder();
                        for (Long costItem : swfLastAprvReqAddCostDTO.getCostItem()) {
                            buf.append(costItem.toString().concat(","));
                        }
                        swfLastAprvReqEntity.setCondition1(buf.toString().substring(0, buf.toString().length() - 1));
                    } else if (swfLastAprvReqAddCostDTO.getCostItem().size() == 1) {
                        swfLastAprvReqEntity.setCondition1(swfLastAprvReqAddCostDTO.getCostItem().get(0).toString());
                    } else {
                        swfLastAprvReqEntity.setCondition1(null);
                    }
                    if (swfLastAprvReqAddCostDTO.getPosnIdList().size() > 1) {
                        StringBuilder buf1 = new StringBuilder();
                        for (Long posn : swfLastAprvReqAddCostDTO.getPosnIdList()) {
                            buf1.append(posn.toString().concat(","));
                        }
                        swfLastAprvReqEntity.setRequirement1(buf1.toString().substring(0, buf1.toString().length() - 1));
                    } else if (swfLastAprvReqAddCostDTO.getPosnIdList().size() == 1) {
                        swfLastAprvReqEntity.setRequirement1(swfLastAprvReqAddCostDTO.getPosnIdList().get(0).toString());
                    } else {
                        swfLastAprvReqEntity.setRequirement1(null);
                    }
                    swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                    baseDao.insert(swfLastAprvReqEntity);
                }
            } else {
                SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTO.getDocTypeValue());
                swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTO.getCompId());
                swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.POSNREQ.value());
                swfLastAprvReqEntity.setCondType(SwfLastAprvCondTypeEnum.COSTREQ.value());
                swfLastAprvReqEntity.setPrtyLevel(swfLastAprvReqAddDataDTO.getData().getCostNum());
                swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                baseDao.insert(swfLastAprvReqEntity);
            }
            //金额要求
            if (swfLastAprvReqAddDataDTO.getData().getMoneyData().size() > 0) {
                StringBuilder buf1 = new StringBuilder();
                for (SwfLastAprvReqAddMoneyDTO swfLastAprvReqAddMoneyDTO : swfLastAprvReqAddDataDTO.getData().getMoneyData()) {
                    SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                    swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTO.getDocTypeValue());
                    swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTO.getCompId());
                    swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                    swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.POSNREQ.value());
                    swfLastAprvReqEntity.setCondType(SwfLastAprvCondTypeEnum.MONRYREQ.value());
                    swfLastAprvReqEntity.setPrtyLevel(swfLastAprvReqAddDataDTO.getData().getMoneyNum());
                    if (swfLastAprvReqAddMoneyDTO.getPosnIdList2().size() > 1) {
                        for (Long posn : swfLastAprvReqAddMoneyDTO.getPosnIdList2()) {
                            buf1.append(posn.toString().concat(","));
                        }
                        swfLastAprvReqEntity.setRequirement1(buf1.toString().substring(0, buf1.toString().length() - 1));
                    } else if (swfLastAprvReqAddMoneyDTO.getPosnIdList2().size() == 1) {
                        swfLastAprvReqEntity.setRequirement1(swfLastAprvReqAddMoneyDTO.getPosnIdList2().get(0).toString());
                    } else {
                        swfLastAprvReqEntity.setRequirement1(null);
                    }
                    swfLastAprvReqEntity.setCondition1(swfLastAprvReqAddMoneyDTO.getMoney());

                    swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                    baseDao.insert(swfLastAprvReqEntity);
                }
            } else {
                SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTO.getDocTypeValue());
                swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTO.getCompId());
                swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.POSNREQ.value());
                swfLastAprvReqEntity.setCondType(SwfLastAprvCondTypeEnum.MONRYREQ.value());
                swfLastAprvReqEntity.setPrtyLevel(swfLastAprvReqAddDataDTO.getData().getMoneyNum());
                swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                baseDao.insert(swfLastAprvReqEntity);
            }

            //审批段定义
            SwfDocPhaseDefEntity swfDocPhaseDefEntity = new SwfDocPhaseDefEntity();
            swfDocPhaseDefEntity.setIsDeleted(0);
            swfDocPhaseDefEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
            swfDocPhaseDefEntity.setDocTypeValue(swfLastAprvReqAddDTO.getDocTypeValue());
            swfDocPhaseDefEntity.setCompId(swfLastAprvReqAddDTO.getCompId());
            swfDocPhaseDefEntity.setSortNum(i);
            swfDocPhaseDefEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            swfDocPhaseDefService.insert(swfDocPhaseDefEntity);
            i++;
        }
    }

    /**
     * 根据公司和单据类型,来获取审批段设置
     *
     * @param compId       公司id
     * @param docTypeValue 单据类型
     * @return com.softtown.modules.dto.SwfLastAprvReqAddDTO
     * @Author Dingyayong
     * @Date 2020/4/13 17:51
     */
    @Override
    public SwfLastAprvReqAddDTO getSwfLastAprvReqInfo(Long compId, String docTypeValue) {
        //整个页面数据
        SwfLastAprvReqAddDTO swfLastAprvReqAddDTO = new SwfLastAprvReqAddDTO();
        //整个tableData 数据
        List<SwfLastAprvReqAddDataDTO> list = new ArrayList<>();
        List<SwfDocPhaseDefEntity> swfDocPhaseDefEntities = swfDocPhaseDefService.getDocPhaseDefListByDocType(compId, docTypeValue);
        for (SwfDocPhaseDefEntity swfDocPhaseDefEntity:swfDocPhaseDefEntities) {
            SwfLastAprvReqAddDataDTO swfLastAprvReqAddDataDTO = new SwfLastAprvReqAddDataDTO();
            //最终要求
            SwfLastAprvReqDataDTO swfLastAprvReqDataDTO = new SwfLastAprvReqDataDTO();
            swfLastAprvReqAddDataDTO.setApproval(swfDocPhaseDefEntity.getPhaseId());
            swfLastAprvReqAddDataDTO.setNum(swfDocPhaseDefEntity.getSortNum());
            //找出 公司 单据类型 审批段 对应的部门最终审资格设置
            SwfLastAprvReqEntity swfLastAprvReqEntity
                    = baseDao.findLastAprvReqByPhaseAndDocType(compId, swfDocPhaseDefEntity.getPhaseId(), docTypeValue);
            if (swfLastAprvReqEntity != null) {
                // 部门
                List<Long> deptIdList = new ArrayList<>();
                // 判断要求1为2  自定义部门时
                if (swfLastAprvReqEntity.getRequirement1().equals(SwfLastAprvReqEnum.DEPTBYSELF.value())) {
                    if (swfLastAprvReqEntity.getRequirement2().contains(",")) {
                        String[] deptList = swfLastAprvReqEntity.getRequirement2().split(",");
                        for (String dept : deptList) {
                            deptIdList.add(Long.valueOf(dept));
                        }
                    } else {
                        deptIdList.add(Long.valueOf(swfLastAprvReqEntity.getRequirement2()));
                    }
                    swfLastAprvReqDataDTO.setRadio(SwfLastAprvReqEnum.DEPTBYSELF.value());
                }
                //判断要求1为1  本部门或者上级部门
                if (swfLastAprvReqEntity.getRequirement1().equals(SwfLastAprvReqEnum.DEPTPARENT.value())) {
                    swfLastAprvReqDataDTO.setRadio(SwfLastAprvReqEnum.DEPTPARENT.value());
                }
                //判断要求1为0  无要求
                if (swfLastAprvReqEntity.getRequirement1().equals(SwfLastAprvReqEnum.DEPTREQNO.value())) {
                    swfLastAprvReqDataDTO.setRadio(SwfLastAprvReqEnum.DEPTREQNO.value());
                }
                swfLastAprvReqDataDTO.setDeptIdList(deptIdList);
            }
            //找出 公司 单据类型 审批段 对应的岗位最终审资格设置
            List<SwfLastAprvReqEntity> swfLastAprvReqEntityList
                    = baseDao.findLastAprvReqByPhaseAndDocTypeAndReq(compId, swfDocPhaseDefEntity.getPhaseId(), docTypeValue);
            //金额要求
            List<SwfLastAprvReqAddMoneyDTO> dtoArrayList = new ArrayList<>();
            //费目要求
            List<SwfLastAprvReqAddCostDTO> arrayList = new ArrayList<>();
            if (swfLastAprvReqEntityList.size() > 0) {

                for (SwfLastAprvReqEntity swfLastAprvReqEntity1 : swfLastAprvReqEntityList) {
                    //判断条件类型是金额
                    if (swfLastAprvReqEntity1.getCondType().equals(SwfLastAprvCondTypeEnum.MONRYREQ.value())) {

                        // 金额岗位
                        List<Long> moneyPosnIdList = new ArrayList<>();
                        SwfLastAprvReqAddMoneyDTO swfLastAprvReqAddMoneyDTO = new SwfLastAprvReqAddMoneyDTO();
                        swfLastAprvReqDataDTO.setMoneyNum(swfLastAprvReqEntity1.getPrtyLevel());
                        if (swfLastAprvReqEntity1.getRequirement1() != null) {
                            if (!swfLastAprvReqEntity1.getRequirement1().contains(",")) {
                                moneyPosnIdList.add(Long.valueOf(swfLastAprvReqEntity1.getRequirement1()));
                            } else {
                                String[] posnList = swfLastAprvReqEntity1.getRequirement1().split(",");
                                for (String mPosn : posnList) {
                                    moneyPosnIdList.add(Long.valueOf(mPosn));
                                }
                            }
                        }
                        swfLastAprvReqAddMoneyDTO.setMoney(swfLastAprvReqEntity1.getCondition1());
                        swfLastAprvReqAddMoneyDTO.setPosnIdList2(moneyPosnIdList);
                        dtoArrayList.add(swfLastAprvReqAddMoneyDTO);
                        swfLastAprvReqDataDTO.setMoneyData(dtoArrayList);
                    } else {
                        swfLastAprvReqDataDTO.setMoneyData(dtoArrayList);
                    }
                    //判断条件类型是岗位
                    if (swfLastAprvReqEntity1.getCondType().equals(SwfLastAprvCondTypeEnum.COSTREQ.value())) {
                        // 费目
                        List<Long> costItem = new ArrayList<>();
                        // 费目岗位
                        List<Long> costPosnIdList = new ArrayList<>();
                        SwfLastAprvReqAddCostDTO swfLastAprvReqAddCostDTO = new SwfLastAprvReqAddCostDTO();
                        swfLastAprvReqDataDTO.setCostNum(swfLastAprvReqEntity1.getPrtyLevel());
                        if (swfLastAprvReqEntity1.getCondition1() != null) {
                            if (swfLastAprvReqEntity1.getCondition1().contains(",")) {
                                String[] costList = swfLastAprvReqEntity1.getCondition1().split(",");
                                for (String cost : costList) {
                                    costItem.add(Long.valueOf(cost));
                                }
                            } else {
                                costItem.add(Long.valueOf(swfLastAprvReqEntity1.getCondition1()));
                            }
                        }
                        if (swfLastAprvReqEntity1.getRequirement1() != null) {
                            if (swfLastAprvReqEntity1.getRequirement1().contains(",")) {
                                String[] posnList = swfLastAprvReqEntity1.getRequirement1().split(",");
                                for (String posn : posnList) {
                                    costPosnIdList.add(Long.valueOf(posn));
                                }
                            } else {
                                costPosnIdList.add(Long.valueOf(swfLastAprvReqEntity1.getRequirement1()));
                            }
                        }
                        swfLastAprvReqAddCostDTO.setPosnIdList(costPosnIdList);
                        swfLastAprvReqAddCostDTO.setCostItem(costItem);
                        arrayList.add(swfLastAprvReqAddCostDTO);
                        swfLastAprvReqDataDTO.setCostData(arrayList);
                    } else {
                        swfLastAprvReqDataDTO.setCostData(arrayList);
                    }
                }
            } else {
                swfLastAprvReqDataDTO.setMoneyData(dtoArrayList);
                swfLastAprvReqDataDTO.setCostData(arrayList);
            }
            swfLastAprvReqAddDataDTO.setData(swfLastAprvReqDataDTO);
            list.add(swfLastAprvReqAddDataDTO);
        }
        swfLastAprvReqAddDTO.setTableData(list);
        return swfLastAprvReqAddDTO;
    }

    /**
     * 根据公司和审批段id查找借款审批的部门要求
     *
     * @param compId  公司id
     * @param phaseId 审批段id
     * @return com.softtown.modules.entity.SwfLastAprvReqEntity
     * @author fuming
     * @date 2019/10/30 15:01
     */
    @Override
    public SwfLastAprvReqEntity findLastAprvReqByDeptAndAdv(Long compId, Long phaseId) {
        return baseDao.findLastAprvReqByDeptAndAdv(compId, phaseId);
    }

    /**
     * 根据公司和审批段id条件为费目来查找借款审批的岗位要求
     *
     * @param compId  公司id
     * @param phaseId 审批段id
     * @return java.util.List<com.softtown.modules.entity.SwfLastAprvReqEntity>
     * @author fuming
     * @date 2019/10/30 15:06
     */
    @Override
    public List<SwfLastAprvReqEntity> findLastAprvReqByItemAndAdv(Long compId, Long phaseId) {
        return baseDao.findLastAprvReqByItemAndAdv(compId, phaseId);
    }

    /**
     * 根据公司和审批段id条件为金额来查找借款审批的岗位要求
     *
     * @param compId  公司id
     * @param phaseId 审批段id
     * @return java.util.List<com.softtown.modules.entity.SwfLastAprvReqEntity>
     * @author fuming
     * @date 2019/10/30 15:11
     */
    @Override
    public List<SwfLastAprvReqEntity> findLastAprvReqByMoneyAndAdv(Long compId, Long phaseId) {
        return baseDao.findLastAprvReqByMoneyAndAdv(compId, phaseId);
    }

    /**
     * 修改最终审设置
     *
     * @param swfLastAprvReqAddDTOList 修改终审者资格要求DTO
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 17:53
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editSwfLastAprvReq(SwfLastAprvReqAddDTO swfLastAprvReqAddDTOList) {
        List<SwfLastAprvReqEntity> swfLastAprvReqEntities
                = baseDao.getSwfLastAprvReqInfo(swfLastAprvReqAddDTOList.getCompId(), swfLastAprvReqAddDTOList.getDocTypeValue());
        for (SwfLastAprvReqEntity swfLastAprvReqEntity : swfLastAprvReqEntities) {
            swfLastAprvReqEntity.setIsDeleted(null);
            swfLastAprvReqEntity.setUpdater(SecurityUser.getUserId());
            swfLastAprvReqEntity.setUpdateDate(new Date());
            swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            baseDao.updateLastAprvReq(swfLastAprvReqEntity);
        }
        List<SwfDocPhaseDefEntity> swfDocPhaseDefEntities =
                swfDocPhaseDefService.getDocPhaseDefListByDocType(swfLastAprvReqAddDTOList.getCompId(), swfLastAprvReqAddDTOList.getDocTypeValue());
        for (SwfDocPhaseDefEntity swfDocPhaseDefEntity : swfDocPhaseDefEntities) {
            swfDocPhaseDefEntity.setIsDeleted(null);
            swfDocPhaseDefEntity.setUpdater(SecurityUser.getUserId());
            swfDocPhaseDefEntity.setUpdateDate(new Date());
            swfDocPhaseDefEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            swfDocPhaseDefService.updateDocPhaseDef(swfDocPhaseDefEntity);
        }
        int i = 1;
        for (SwfLastAprvReqAddDataDTO swfLastAprvReqAddDataDTO : swfLastAprvReqAddDTOList.getTableData()) {
            //部门要求
            if (swfLastAprvReqAddDataDTO.getData().getRadio().equals(SwfLastAprvReqEnum.DEPTREQNO.value())) {
                SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTOList.getCompId());
                swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.DEPTREQ.value());
                swfLastAprvReqEntity.setRequirement1(SwfLastAprvReqEnum.DEPTREQNO.value());
                swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTOList.getDocTypeValue());
                swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                baseDao.insert(swfLastAprvReqEntity);
            }
            if (swfLastAprvReqAddDataDTO.getData().getRadio().equals(SwfLastAprvReqEnum.DEPTPARENT.value())) {
                SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                swfLastAprvReqEntity.setRequirement1(SwfLastAprvReqEnum.DEPTPARENT.value());
                swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTOList.getDocTypeValue());
                swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTOList.getCompId());
                swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.DEPTREQ.value());
                swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                baseDao.insert(swfLastAprvReqEntity);
            }
            if (swfLastAprvReqAddDataDTO.getData().getRadio().equals(SwfLastAprvReqEnum.DEPTBYSELF.value())) {
                StringBuilder buf = new StringBuilder();
                SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTOList.getDocTypeValue());
                swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.DEPTREQ.value());
                swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTOList.getCompId());
                swfLastAprvReqEntity.setRequirement1(SwfLastAprvReqEnum.DEPTBYSELF.value());
                for (Long dept : swfLastAprvReqAddDataDTO.getData().getDeptIdList()) {
                    buf.append(dept.toString().concat(","));
                }
                swfLastAprvReqEntity.setRequirement2(buf.toString().substring(0, buf.toString().length() - 1));
                swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                baseDao.insert(swfLastAprvReqEntity);
            }
            //岗位要求
            //费目要求
            if (swfLastAprvReqAddDataDTO.getData().getCostData().size() > 0) {
                for (SwfLastAprvReqAddCostDTO swfLastAprvReqAddCostDTO : swfLastAprvReqAddDataDTO.getData().getCostData()) {
                    SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                    swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTOList.getDocTypeValue());
                    swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTOList.getCompId());
                    swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                    swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.POSNREQ.value());
                    swfLastAprvReqEntity.setCondType(SwfLastAprvCondTypeEnum.COSTREQ.value());
                    swfLastAprvReqEntity.setPrtyLevel(swfLastAprvReqAddDataDTO.getData().getCostNum());
                    if (swfLastAprvReqAddCostDTO.getCostItem().size() > 1) {
                        StringBuilder buf = new StringBuilder();
                        for (Long costItem : swfLastAprvReqAddCostDTO.getCostItem()) {
                            buf.append(costItem.toString().concat(","));
                        }
                        swfLastAprvReqEntity.setCondition1(buf.toString().substring(0, buf.toString().length() - 1));
                    } else if (swfLastAprvReqAddCostDTO.getCostItem().size() == 1) {
                        swfLastAprvReqEntity.setCondition1(swfLastAprvReqAddCostDTO.getCostItem().get(0).toString());
                    } else {
                        swfLastAprvReqEntity.setCondition1(null);
                    }
                    if (swfLastAprvReqAddCostDTO.getPosnIdList().size() > 1) {
                        StringBuilder buf1 = new StringBuilder();
                        for (Long posn : swfLastAprvReqAddCostDTO.getPosnIdList()) {
                            buf1.append(posn.toString().concat(","));
                        }
                        swfLastAprvReqEntity.setRequirement1(buf1.toString().substring(0, buf1.toString().length() - 1));
                    } else if (swfLastAprvReqAddCostDTO.getPosnIdList().size() == 1) {
                        swfLastAprvReqEntity.setRequirement1(swfLastAprvReqAddCostDTO.getPosnIdList().get(0).toString());
                    } else {
                        swfLastAprvReqEntity.setRequirement1(null);
                    }
                    swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                    baseDao.insert(swfLastAprvReqEntity);
                }
            } else {
                SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTOList.getDocTypeValue());
                swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTOList.getCompId());
                swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.POSNREQ.value());
                swfLastAprvReqEntity.setCondType(SwfLastAprvCondTypeEnum.COSTREQ.value());
                swfLastAprvReqEntity.setPrtyLevel(swfLastAprvReqAddDataDTO.getData().getCostNum());
                swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                baseDao.insert(swfLastAprvReqEntity);
            }
            //金额要求
            if (swfLastAprvReqAddDataDTO.getData().getMoneyData().size() > 0) {
                StringBuilder buf1 = new StringBuilder();
                for (SwfLastAprvReqAddMoneyDTO swfLastAprvReqAddMoneyDTO : swfLastAprvReqAddDataDTO.getData().getMoneyData()) {
                    SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                    swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTOList.getDocTypeValue());
                    swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTOList.getCompId());
                    swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                    swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.POSNREQ.value());
                    swfLastAprvReqEntity.setCondType(SwfLastAprvCondTypeEnum.MONRYREQ.value());
                    swfLastAprvReqEntity.setPrtyLevel(swfLastAprvReqAddDataDTO.getData().getMoneyNum());
                    if (swfLastAprvReqAddMoneyDTO.getPosnIdList2().size() > 1) {
                        for (Long posn : swfLastAprvReqAddMoneyDTO.getPosnIdList2()) {
                            buf1.append(posn.toString().concat(","));
                        }
                        swfLastAprvReqEntity.setRequirement1(buf1.toString().substring(0, buf1.toString().length() - 1));
                    } else if (swfLastAprvReqAddMoneyDTO.getPosnIdList2().size() == 1) {
                        swfLastAprvReqEntity.setRequirement1(swfLastAprvReqAddMoneyDTO.getPosnIdList2().get(0).toString());
                    } else {
                        swfLastAprvReqEntity.setRequirement1(null);
                    }
                    swfLastAprvReqEntity.setCondition1(swfLastAprvReqAddMoneyDTO.getMoney());
                    swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                    baseDao.insert(swfLastAprvReqEntity);
                }
            } else {
                SwfLastAprvReqEntity swfLastAprvReqEntity = new SwfLastAprvReqEntity();
                swfLastAprvReqEntity.setDocTypeValue(swfLastAprvReqAddDTOList.getDocTypeValue());
                swfLastAprvReqEntity.setCompId(swfLastAprvReqAddDTOList.getCompId());
                swfLastAprvReqEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
                swfLastAprvReqEntity.setReqType(SwfLastAprvReqTypeEnum.POSNREQ.value());
                swfLastAprvReqEntity.setCondType(SwfLastAprvCondTypeEnum.MONRYREQ.value());
                swfLastAprvReqEntity.setPrtyLevel(swfLastAprvReqAddDataDTO.getData().getMoneyNum());
                swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
                baseDao.insert(swfLastAprvReqEntity);
            }
            //审批段定义
            SwfDocPhaseDefEntity swfDocPhaseDefEntity = new SwfDocPhaseDefEntity();
            swfDocPhaseDefEntity.setIsDeleted(0);
            swfDocPhaseDefEntity.setPhaseId(swfLastAprvReqAddDataDTO.getApproval());
            swfDocPhaseDefEntity.setDocTypeValue(swfLastAprvReqAddDTOList.getDocTypeValue());
            swfDocPhaseDefEntity.setCompId(swfLastAprvReqAddDTOList.getCompId());
            swfDocPhaseDefEntity.setSortNum(i);
            swfDocPhaseDefEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            swfDocPhaseDefService.insert(swfDocPhaseDefEntity);
            i++;
        }
    }

    /**
     * 删除最终审设置
     *
     * @param swfLastAprvReqDTO 终审者资格要求DTO
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 17:54
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSwfLastAprvReq(SwfLastAprvReqDTO swfLastAprvReqDTO) {
        List<SwfLastAprvReqEntity> swfLastAprvReqEntities
                = baseDao.getSwfLastAprvReqInfo(swfLastAprvReqDTO.getCompId(), swfLastAprvReqDTO.getDocTypeValue());
        for (SwfLastAprvReqEntity swfLastAprvReqEntity : swfLastAprvReqEntities) {
            swfLastAprvReqEntity.setIsDeleted(null);
            swfLastAprvReqEntity.setUpdateDate(new Date());
            swfLastAprvReqEntity.setUpdater(SecurityUser.getUserId());
            swfLastAprvReqEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            baseDao.updateLastAprvReq(swfLastAprvReqEntity);
        }
        List<SwfDocPhaseDefEntity> swfDocPhaseDefEntities =
                swfDocPhaseDefService.getDocPhaseDefListByDocType(swfLastAprvReqDTO.getCompId(), swfLastAprvReqDTO.getDocTypeValue());
        for (SwfDocPhaseDefEntity swfDocPhaseDefEntity : swfDocPhaseDefEntities) {
            swfDocPhaseDefEntity.setUpdateTimestamp(String.valueOf(System.currentTimeMillis()));
            swfDocPhaseDefEntity.setIsDeleted(null);
            swfDocPhaseDefEntity.setUpdater(SecurityUser.getUserId());
            swfDocPhaseDefEntity.setUpdateDate(new Date());
            swfDocPhaseDefService.updateDocPhaseDef(swfDocPhaseDefEntity);
        }
    }
}
